@extends('Admin.layout.layout')
@section('content')
    <style>
        .recProduct{
            margin: 0 5px;
        }
    </style>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>商品管理</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="tabs-container">
                            <ul class="nav nav-tabs" id="navState">
                                <li <?php if(  $search_params['is_for_sale']!= "0" && $search_params['status'] != "0"){?>class="active" <?php }?> data-id="0">
                                    <a    href="{{Request::url()}}" >全部</a>
                                </li>
                                <li  <?php if(  $search_params['is_for_sale']== "0"){?>class="active" <?php }?>data-id="1">
                                    <a href="{{Request::url()}}?is_for_sale=0">违规下架 </a>
                                </li>
                                <li <?php if(  $search_params['status']== "0"){?>class="active" <?php }?> data-id="2">
                                    <a href="{{Request::url()}}?status=0">待审核 </a>
                                </li>
                            </ul>
                        <div class="ibox-content" style="border-top: 0;">
                            <div class="row">
                            <form action="" method="GET">
                                <div class="col-sm-12">
                                    <div class="input-group fl mr10 ">
                                        <input type="text" placeholder="请输入商品名称" class="input-sm form-control" name="goods_name" value="{{$search_params['goods_name']}}">
                                    </div>

                                    <div class="input-group fl mr10 ">
                                        <input type="text" placeholder="请输入商品编号" class="input-sm form-control" name="goods_id" value="{{$search_params['goods_id']}}">
                                    </div>

                                    <div class="input-group fl mr10 ">
                                        <input type="text" placeholder="输入商品所属店铺名称" class="input-sm form-control" name="sp_name" value="{{$search_params['sp_name']}}">
                                    </div>

                                    <div class="input-group fl mr10 ">
                                        <select style="float: left; margin-right: 10px;" title="" class="input-sm form-control"  name="first_cat"  id="first_cat">
                                            <option value="">请选择</option>
                                            @foreach($cates  as $key => $cate)
                                                @if($cate['pid'] == 0 )
                                                <option value="{{$cate['id']}}" @if($cate['id'] == $search_params['first_cat']) selected="selected"@endif  >
                                                    {{$cate['name']}}
                                                </option>
                                                @endif
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="input-group fl mr10 ">
                                        <select style="float: left; margin-right: 10px;" class="input-sm form-control" title="" name="second_cat"  id="second_cat">
                                        <option value="">请选择</option>
                                            @foreach($cates  as $key => $cate)
                                                @if($cate['pid'] == $search_params['first_cat']  && $search_params['first_cat'] )
                                                    <option value="{{$cate['id']}}"   @if($cate['id'] == $search_params['second_cat']) selected="selected"@endif>
                                                        {{$cate['name']}}
                                                    </option>
                                                @endif
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="input-group fl mr10 ">
                                        <select style="float: left; margin-right: 10px;padding:0;" class="input-sm form-control" title="" name="product_type"  id="second_cat">
                                            <option value="">全部</option>
                                            <option value="1" @if($search_params['product_type']=='1') selected @endif>普通商品</option>
                                            <option value="2" @if($search_params['product_type']=='2') selected @endif>资质服务</option>
                                        </select>
                                    </div>
                                    <div class="input-group fl mr10 ">
                                        <select style="float: left; margin-right: 10px;padding:0;" class="input-sm form-control" title="" name="is_for_sale"  id="is_for_sale">
                                            <option value="">请选择</option>
                                            <option value="1" @if($search_params['is_for_sale']=='1') selected @endif>上架</option>
                                            <option value="0" @if($search_params['is_for_sale']=='0') selected @endif>下架</option>
                                        </select>
                                    </div>
                                    <div class="input-group fl mr10 ">
                                        <select style="float: left; margin-right: 10px;padding:0;" class="input-sm form-control" title="" name="is_index_recommend"  id="is_for_sale">
                                            <option value="">首页推荐</option>
                                            <option value="1" @if($search_params['is_index_recommend']=='1') selected @endif>已推荐</option>
                                            <option value="0" @if($search_params['is_index_recommend']=='0') selected @endif>未推荐</option>
                                        </select>
                                    </div>
                                    <button type="submit" class="btn btn-sm btn-primary fl">查询</button>
                                </div>
                                </form>
                                 <input type="hidden" name="cates_json" id="cates_json" value="{{$cates_json}}" />
                            </div>
                            <div class="tab-content">
                                <div class="batch-check" style="padding:10px 10px 0 20px;">
                                       <span>批量操作:</span>
                                        <select  title="" class="status_batch">
                                            <option value="-1">请选择</option>
                                            <option value="0">未审核</option>
                                            <option value="1">已审核</option>
                                            <option value="2">未通过</option>
                                        </select>
                                </div>
                                <div id="tab-1" class="tab-pane active">
                                    <div class="panel-body">
                                        <table class="table table-striped table-bordered table-hover dataTable" id="editable">
                                            <thead>
                                                <tr>
                                                    <th width="15">
                                                         <input type="checkbox" class="checkall" name="selectall" />
                                                    </th>
                                                    <th>商品ID</th>
                                                    <th>商品名称</th>
                                                    <th>主图</th>
                                                    <th>店铺名</th>
                                                    <th class="center">状态</th>
                                                    <th class="center">更新时间</th>
                                                    <th class="center">操作</th>
                                                </tr>
                                            </thead>
                                            @foreach($list  as $key => $product)
                                                <tbody>
                                                <tr>
                                                    <th>
                                                        <input type="checkbox" title="" class="checkall" name="checkBox" data-id="{{$product['id']}}"/>
                                                    </th>
                                                    <td>{{$product['id']}}</td>
                                                    @if($product['status'] == 1 ) <td><a href="/product/info/{{$product['id']}}/0" target="_blank"> {{$product['name']}}</a></td>
                                                    @else
                                                    <td>  {{$product['name']}} </td>
                                                    @endif
                                                   
                                                    <td>
                                                        <img width="60" height="60" src="{{$product['cover_image']}}">
                                                    </td>
                                                    <td class="center"><a href="/foradmin/user/{{$product['user_id']}}" target="_blank" class="shop-name" >{{$product['shop']['sp_name']}}</a></td>
                                                    <td class="center">@if($product['is_for_sale'] == 1)上架@else下架@endif</td>
                                                    <td class="center">{{$product['updated_at']}}</td>
                                                    <td class="center">
                                                        <div class="dropdown">
                                                            <select  title="" data-id="{{$product['id']}}" style="float: left; margin-right: 10px;" class="status_search">
                                                                <option value="0" @if($product['status'] == 0 )selected="selected"@endif>未审核</option>
                                                                <option value="1" @if($product['status'] == 1 )selected="selected"@endif>已审核</option>
                                                                <option value="2" @if($product['status'] == 2 )selected="selected"@endif>未通过</option>
                                                            </select>
                                                            <!--  <div class="see" style="float:left;color:#0000ff;">查看</div> -->
                                                            <a class="btn-xs btn-primary check_details" data-id="{{$product['id']}}" style="float:left;">查看</a>
                                                            @if($product['is_for_sale'] == 1)
                                                                <div class="force1" data-id="{{$product['id']}}" data-type="1"style="float:left;margin: 0 10px;"  data-value="0"><a>强制下架</a></div>
                                                            @else
                                                                <div class="force2" data-id="{{$product['id']}}" data-type="1"style="float:left;margin: 0 10px;"  data-value="1"><a>上架</a></div>
                                                            @endif

                                                            @if($product['is_recommend'] == 1)
                                                                <div class="recommend1" data-id="{{$product['id']}}" data-type="2"style="float:left;color:red;" data-value="0"><a>取消推荐</a></div>
                                                            @else
                                                                <div class="recommend2" data-id="{{$product['id']}}" data-type="2"style="float:left;color:red;"  data-value="1"><a>推荐</a></div>
                                                            @endif
                                                            @if($product['is_index_recommend'] == 1)
                                                                <a class="btn-xs btn-primary rec_{{$product['id']}}" data-toggle="modal" data-target="#recommendModal"  data-id="{{$product['id']}}" data-rec="{{$product['is_index_recommend']}}" data-name="{{$product['name']}}" style="float:left;" data-spname="{{$product['shop']['sp_name']}}" data-sort="{{isset($product['index_sort'])?$product['index_sort']:2}}">取消至首页</a>
                                                            @else
                                                                <a class="btn-xs btn-primary rec_{{$product['id']}}" data-toggle="modal" data-target="#recommendModal"  data-id="{{$product['id']}}" data-rec="{{$product['is_index_recommend']}}" data-name="{{$product['name']}}" style="float:left;" data-spname="{{$product['shop']['sp_name']}}" data-sort="{{isset($product['index_sort'])?$product['index_sort']:2}}">推荐至首页</a>
                                                            @endif
                                                        </div>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            @endforeach
                                        </table>
                                    </div>
                                    <div class="row">
                                        @include('Admin.common.paginate', ['page' => $page])
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="modal fade" id="recommendModal" tabindex="-1" role="dialog" aria-labelledby="recommendModal" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">×
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        推荐至首页
                    </h4>
                </div>
                <form class="form-horizontal m-t" id="editForm">
                    <input type="hidden" title="" name="id"/>
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品名称：</label>
                            <div class="col-sm-8">
                                <p class="form-control-static" id="name"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">所属店铺：</label>
                            <div class="col-sm-8">
                                <p class="form-control-static" id="sp_name"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">推荐状态：</label>
                            <div class="col-sm-8">
                                <div class="radiolist i-checks">
                                    <label><input type="radio" name="is_index_recommend" value="1"/>设为推荐</label>
                                    <label><inpuT type="radio" name="is_index_recommend" value="0"/>取消推荐</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">首页排序：</label>
                            <div class="col-sm-7">
                                <input type="number" name="index_sort" value="" class="input-sm form-control" placeholder="首页推荐商品排序，数字越大越靠前" min="1" title="首页排序">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary recProduct" id="btn_submit">
                            确定
                        </button>
                        <button type="button" class="btn btn-default closeModal"
                                data-dismiss="modal" >关闭
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="{{asset('js/plugins/layer/layer.min.js')}}"></script>
    <script src="{{asset('js/plugins/switchery/switchery.js')}}"></script>

    <script>
        $('.shop-name').on('click',function(e){
            e.preventDefault()
            var href=$(this).attr("href")
            layer.open({
              type: 2,
              area: ['100%', '100%'],
              fixed: false, //不固定
              maxmin: true,
              content: href
            });
        })
        $(document).ready(function () {
            var edit_id = 0;//修改属性的id
            // validate signup form on keyup and submit
            var icon = "<i class='fa fa-times-circle'></i> ";

			// $('.force').click(function(){
			// 	edit_id = $(this).attr("data-id");//编辑时属性的id
   //              var is_sale = $(this).attr("data-value");
   //          	$.ajax({
   //                  url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
   //                  type: 'put',
   //                  dataType: 'json',
   //                  data: {'is_for_sale': is_sale},
   //                  success: function (data) {
   //                      if (0 == data.code) {
   //                         location.reload();
   //                      } else {
   //                          layer.alert(data.message, {
   //                              icon: 2
   //                          });
   //                      }
   //                  }
   //              });
   //          })
            $('.force2').click(function(){
                edit_id = $(this).attr("data-id");//编辑时属性的id
                var is_sale = $(this).attr("data-value");
                layer.confirm('是否上架？',{
                    btn: ['确认', '取消'] //可以无限个按钮
                },function() {
                    $.ajax({
                        url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
                        type: 'put',
                        dataType: 'json',
                        data: {'is_for_sale': is_sale},
                        success: function (data) {
                            if (0 == data.code) {
                               layer.alert('上架成功', {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                })
            })
            $('.force1').click(function(){
                edit_id = $(this).attr("data-id");//编辑时属性的id
                var is_sale = $(this).attr("data-value");
                layer.confirm('是否强制下架？',{
                    btn: ['确认', '取消'] //可以无限个按钮
                },function() {
                    $.ajax({
                        url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
                        type: 'put',
                        dataType: 'json',
                        data: {'is_for_sale': is_sale},
                        success: function (data) {
                            if (0 == data.code) {
                               layer.alert('强制下架成功', {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                })
            })
            // $('.recommend').click(function(){
            //     edit_id = $(this).attr("data-id");//编辑时属性的id
            //     var is_recommend = $(this).attr("data-value");
            //     $.ajax({
            //         url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
            //         type: 'put',
            //         dataType: 'json',
            //         data: {'is_recommend': is_recommend},
            //         success: function (data) {
            //             if (0 == data.code) {
            //                location.reload();
            //             } else {
            //                 layer.alert(data.message, {
            //                     icon: 2
            //                 });
            //             }
            //         }
            //     });
            // })
            $('.recommend1').click(function(){
                edit_id = $(this).attr("data-id");//编辑时属性的id
                var is_recommend = $(this).attr("data-value");
                layer.confirm('是否取消推荐？',{
                   btn: ['确认', '取消'] //可以无限个按钮
                },function(index) {
                        $.ajax({
                        url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
                        type: 'put',
                        dataType: 'json',
                        data: {'is_recommend': is_recommend},
                        success: function (data) {
                            if (0 == data.code) {
                               layer.alert('取消推荐成功', {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                })
            })
            $('.recommend2').click(function(){
                edit_id = $(this).attr("data-id");//编辑时属性的id
                var is_recommend = $(this).attr("data-value");
                layer.confirm('是否推荐？',{
                   btn: ['确认', '取消'] //可以无限个按钮
                },function(index) {
                        $.ajax({
                        url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
                        type: 'put',
                        dataType: 'json',
                        data: {'is_recommend': is_recommend},
                        success: function (data) {
                            if (0 == data.code) {
                               layer.alert('推荐成功', {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                })
            })
            $(".status_search").change(function(){
                edit_id = $(this).attr("data-id");//编辑时属性的id
                var status_val = $(this).val();
                $.ajax({
                    url: '{{Request::url()}}/'+edit_id+"/ajaxupdate",
                    type: 'put',
                    dataType: 'json',
                    data: {'status': status_val},
                    success: function (data) {
                        if (0 == data.code) {
                           location.reload();
                        } else {
                            layer.alert(data.message, {
                                icon: 2
                            });
                        }
                    }
                });
            });


            $(".status_batch").change(function(){
                var ids=[]
                var status_val = $(this).val();
                var items=$('input[name="checkBox"]:checked')
                if(items.length>0){
                    items.each(function(i,item){
                        ids.push($(item).attr("data-id"))
                    })
                    $.ajax({
                        url: '{{Request::url()}}/batchstatus',
                        type: 'post',
                        dataType: 'json',
                        data: {'ids':ids, 'status': status_val},
                        success: function (data) {
                            if (0 == data.code) {
                               location.reload();
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }
                else{
                     layer.alert("未勾选操作项", {icon: 2});
                }
            });

            // 推荐商品至首页 , 首页商品排序字段加在商品列表。
            $('#recommendModal').on('show.bs.modal', function (e) {
                edit_id     = $(e.relatedTarget).attr("data-id"); // 编辑时属性的id
                var sp_name = $(e.relatedTarget).attr("data-spname"); // 编辑时属性的 sp_name
                var name = $(e.relatedTarget).attr("data-name"); // 编辑时属性的 name
                var rec  = $(e.relatedTarget).attr("data-rec"); // 编辑时属性的 rec
                var sort = $(e.relatedTarget).attr("data-sort"); //编辑时属性的 sort

                // 新选中值赋值
                $('input[name="id"]').val(edit_id);
                $('#name').html(name);
                $('#sp_name').html(sp_name);
                $('input[name="index_sort"]').val(sort);
                $('input:radio[name="is_index_recommend"][value='+rec+']').prop("checked",'checked');

            });
            $('.recProduct').click(function(){
                var formdata = $('#editForm').serialize();
                id = $('input[name="id"]').val();
                is_index_recommend = $('input:radio[name="is_index_recommend"]:checked').val();
                sort = $('input[name="index_sort"]').val();
                if (is_index_recommend==1){
                    operation = '取消至首页';
                }else{
                    operation = '推荐至首页';
                }
                console.log(is_index_recommend);
                console.log(operation);
                $.ajax({
                    url: '{{Request::url()}}/'+id+"/recommend",
                    type: 'POST',
                    dataType: 'json',
                    data: formdata,
                    success: function (data) {
                        console.log(data);
                        if (0 == data.code) {
                            layer.alert("修改成功", {
                                icon: 1
                            }, function (index) {
                                // 关闭 modal
                                $('.closeModal').click();
                                // 修改页面信息，不刷新页面。
                                $('.rec_'+id).attr("data-rec",is_index_recommend);
                                $('.rec_'+id).attr("data-sort",sort);
                                $('.rec_'+id).html(operation);
                                layer.close(index);
                            });
                        } else {
                            layer.alert(data.message, {
                                icon: 2
                            });
                        }
                    }
                });
            })
            //查看详情
            $('.check_details').click(function(){
                var url = '{{Request::url()}}/' + $(this).data('id');//前端正式
                var title = '商品详情';
                open_page(url, title);//打开新页面
            })

            $("#first_cat").change(function(){
                var cates_json = eval($("#cates_json").val());
                $("#second_cat").html("<option value='0'>请选择</option>");
                for(c in cates_json){
                    if(cates_json[c]['pid'] == $(this).val()){
                        $("#second_cat").append("<option value='"+cates_json[c]['id']+"'>"+cates_json[c]['name']+"</option>");
                    }
                }
              });
        });


    </script>
@endsection